package com.archerdu.apply.controller.admin;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.ArticleCatalog;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.ArticleCatalogManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.commons.constant.DataConstant;

@Controller
@Scope("prototype")
@RequestMapping(value = "/admin/articleCatalog")
public class ArticleCatalogController
        extends BaseController {
    
    private static final long  serialVersionUID = 4707162553670697575L;
    
    private ArticleCatalogManager articleCatalogManager;
    private ArticleCatalog        articleCatalog;
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/tree")
    public String tree() {
        return "admin/articleCatalog/tree";
    }
    
    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, ArticleCatalogController mc) {
        articleCatalog = mc.getArticleCatalog();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin/articleCatalog/add");
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            mv.addObject(articleCatalog);
        }
        else {
            try {
                articleCatalog.setCreateUser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER))
                        .getId());
                articleCatalog.setCreatetime(getSqlDate());
                articleCatalogManager.add(articleCatalog);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
            }
            catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        return mv;
    }
    
    /**
     * 文章目录编辑
     * 
     * @param requ
     * @param articleCatalog
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, ArticleCatalogController mc) {
        
        logger.debug("articleCatalog.id is " + mc.getArticleCatalog().getId());
        articleCatalog = mc.getArticleCatalog();
        
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(articleCatalog.getId())) {
                articleCatalog = articleCatalogManager.get(articleCatalog.getId());
            }
        }
        else {
            articleCatalog.setUpdateUser(((User) requ.getSession().getAttribute((DataConstant.SESSION_SYS_USER))).getId());
            articleCatalog.setUpdatetime(getSqlDate());
            try {
                articleCatalogManager.modify(articleCatalog);
                articleCatalog = articleCatalogManager.get(articleCatalog.getId());
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            }
            catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        
        ModelAndView mav = new ModelAndView();
        mav.addObject(getMessage());
        mav.addObject(articleCatalog).setViewName("admin/articleCatalog/edit");
        return mav;
    }
    
    /**
     * 目录json字符串
     * 
     * @param response
     * @param writer
     * @param acc
     */
    @RequestMapping(value = "/json")
    public void json(HttpServletResponse response, Writer writer, ArticleCatalogController acc) {
        response.setContentType("text/plain");
        response.setCharacterEncoding("utf-8");
        articleCatalog = acc.getArticleCatalog();
        if (articleCatalog != null && articleCatalog.getId() != null) {
            articleCatalog = articleCatalogManager.get(articleCatalog.getId());
        }
        else {
            articleCatalog = new ArticleCatalog();
            articleCatalog.setId("0");
        }
        try {
            writer.write(articleCatalogManager.loadJson(articleCatalog));
        }
        catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
    
    public ArticleCatalogManager getArticleCatalogManager() {
        return articleCatalogManager;
    }
    
    public void setArticleCatalogManager(ArticleCatalogManager articleCatalogManager) {
        this.articleCatalogManager = articleCatalogManager;
    }
    
    public ArticleCatalog getArticleCatalog() {
        return articleCatalog;
    }
    
    public void setArticleCatalog(ArticleCatalog articleCatalog) {
        this.articleCatalog = articleCatalog;
    }
    
}
